export default class AI extends Laya.Script {
        MinX=1089;
        MaxX=1805;
        /** @prop {name:qiu, tips:"qiu", type:Node, default:null}*/
        qiu=null;
        isjump=true;
        gameover=false;
        rig=null;
        dis=null;
        owner=(this.owner as Laya.Script)
    constructor() { 
        super(); 
        
        
        // 更多参数说明请访问: https://ldc2.layabox.com/doc/?nav=zh-as-2-4-0
    }
    onAwake(){
        Laya.stage.on("GamerOver",this,function(){this.rig.type="static";this.gameover=true;})
        Laya.stage.on("start",this,this.start)
        this.rig=this.owner.getComponent(Laya.RigidBody);
    }
    start(){
        this.rig.type="dynamic";
        this.owner.pos(1522,785);
        this.rig.setVelocity({x:0,y:0})
        this.gameover=false;
    }
    onUpdate(){
        if(this.owner.y<742){
            this.isjump=false;
        }else{
            this.isjump=true;
        }
        if(this.qiu.x>1024&&this.qiu.x<this.MaxX){
            if(this.gameover)return;
            var targetX=this.qiu.x
            this.owner.x=Laya.MathUtil.lerp(this.owner.x,targetX,
            Laya.timer.delta/1000*3)
        }
        var delX=this.owner.x-this.qiu.x;
        var delY=this.owner.y-this.qiu.y;
        this.dis=Math.sqrt(delX*delX+delY*delY)
        
        if(this.dis<180&&this.isjump){
             this.jump();
    }
    
}
    jump(){
                this.isjump=false;
                var x=this.rig.linearVelocity.x;
                var sui=this.getRandom(5,8)
                this.rig.setVelocity({x:x+sui,y:-15});
                this.owner.texture="res/派蒙2.png"
                Laya.SoundManager.playSound("res/sound/AI.wav",1);


    }
    onTriggerEnter(other){
        if(other.label=="qiu"){
            Laya.SoundManager.playSound("res/sound/AI.wav",1);
            this.owner.texture="res/派蒙2.png"
        }
        if(other.label=="di"){
            this.owner.texture="res/派蒙1.png"
        }
    
    }
    onTriggerExit(other){
        if(other.label=="qiu"){
            this.owner.texture="res/派蒙1.png"
        }
    
    }

    getRandom(min,max)
        {
        var value=max-min;
        value=Math.random()*value
        return value+min;
        }
}